package com.android.lazysearch;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class RestaurantAdapter extends BaseExpandableListAdapter{
	DummyValues demo = new DummyValues();
	
	Context mContext;
	private ArrayList<Restaurant> groups;
		
	public RestaurantAdapter(Context c, ArrayList<Restaurant> restaurants)
	{
		mContext = c;
		groups = restaurants;
	}

	public Object getChild(int groupPosition, int childPosition) {
//		return children[groupPosition][childPosition];
		return groups.get(groupPosition).getCheckedoutItem(childPosition);
	}
	
	public long getChildId(int groupPosition, int childPosition) {
	    return childPosition;
	}
	
	public int getChildrenCount(int groupPosition) 
	{
	    int i = 0;
	    try 
	    {
	    	i = groups.get(groupPosition).childCount();	
//	    	i = children[groupPosition].length;
	    } 
	    catch (Exception e) 
	    {
	    }
	
	    return i;
	}

	public TextView getGenericView() 
	{
		// Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

        TextView textView = new TextView(mContext);
        textView.setLayoutParams(lp);
        textView.setPadding(36, 0, 0, 0);
        textView.setTextSize(30);
        textView.setMinHeight(40);
        return textView;		
	}
		
	public View getChildView(int groupPosition, int childPosition,
	        boolean isLastChild, View convertView, ViewGroup parent) 
	{        
		ViewGroup row;
		TextView itemName;
		final TextView itemQty;
		final TextView itemTotal;
		
		if(convertView==null)
		{
			LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = (ViewGroup)mInflater.inflate(R.layout.check_items, null);
		}
		else
		{
			row = (ViewGroup)convertView;
		}
		
			//Set Item Name TextView
			itemName = (TextView)row.getChildAt(0);
			itemName.setText(((Item)getChild(groupPosition,childPosition)).getItemName());

			itemQty = (TextView)row.getChildAt(2);
			itemQty.setText(((Item)getChild(groupPosition,childPosition)).getItemQty().toString());
			
			itemTotal = (TextView)row.getChildAt(4);
			Float totalItemCost = ((Item)getChild(groupPosition,childPosition)).getItemQty() * ((Item)getChild(groupPosition,childPosition)).getItemPrice();
			itemTotal.setText("$"+String.format("%.2f", totalItemCost));
			return row;
			
//			decButton = (Button)row.getChildAt(1);	
//			incButton = (Button)row.getChildAt(3);
//						
//			decButton.setOnClickListener(new View.OnClickListener() {
//				public void onClick(View v) {
//					Integer qty = item.getItemQty();
//					if(qty >= 1)
//					{
//			           	 Float price = item.getItemPrice();
//			           	 Float totalItemCost;
//			            	 
//			           	 qty = qty - 1;
//			           	 item.setItemQty(qty);
//			           	 itemQty.setText(item.getItemQty().toString());
//		
//			           	 totalItemCost = qty * price;
//			           	 Float totalAmt = app.getTotal();
//			           	 totalAmt = totalAmt - price;
//			           	 app.setTotal(totalAmt);
//			           	 itemTotal.setText("$"+String.format("%.2f", totalItemCost));
//			           	 tvTotal.setText("$"+String.format("%.2f", totalAmt));
//					}
//		        }
//		    });
//
//			incButton.setOnClickListener(new View.OnClickListener() {
//	             public void onClick(View v) {
//	            	 
//	            	 Integer qty = item.getItemQty();
//	            	 Float price = item.getItemPrice();
//	            	 Float totalItemCost;
//	            	 
//	            	 qty = qty + 1;
//	            	 item.setItemQty(qty);
//	            	 itemQty.setText(item.getItemQty().toString());
//	            	 
//	            	 totalItemCost = qty * price;
//	            	 Float totalAmt = app.getTotal();
//		           	 totalAmt = totalAmt + price;
//		           	 app.setTotal(totalAmt);
//	            	 itemTotal.setText("$"+String.format("%.2f", totalItemCost));
//		           	 tvTotal.setText("$"+String.format("%.2f", totalAmt));
//	             }
//	         });
	}
	
	public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
//		return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.size();
//    	return groups.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    	
        TextView textView = getGenericView();
        textView.setText(((Restaurant)getGroup(groupPosition)).getRestaurantName());
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(25);
        textView.setHeight(40);
        textView.setBackgroundColor((Color.rgb(8, 154, 189)));
        return textView;
    	
//    	TextView textView = getGenericView();
//        textView.setText(getGroup(groupPosition).toString());
//        return textView;
    }
    
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }
}